#include "../include/CandidateExtractor.h"
Candidate::Candidate()
{

}

CandidateExtractor::CandidateExtractor()
{

}
void CandidateExtractor::setObjects(vector<LSfeatureObject> objts)
{
    for(vector<LSfeatureObject>::iterator iit = objts.begin();iit != objts.end();iit++)
        if((iit)->confidenceFactor > 0.4 )
        {
            objects.push_back(*(iit)/*->CopyTo()*/);
        }

}
void CandidateExtractor::doExtractor( double minDist)
{
    float closest_dist;
    for ( vector<LSfeatureObject>::iterator it1 = objects.begin(); it1!= objects.end();it1++ )
    {
        vector<LSfeatureObject>::iterator it2 = it1;

        for (++it2 ; it2 != objects.end(); it2++ )
        {
            closest_dist=(it1)->getMiddlePoint().getDistanceFromPoint((it2)->getMiddlePoint());
            if( closest_dist < minDist)
            {
                it2->valid = true;

            }

        }
    }
    for ( vector<LSfeatureObject>::iterator it1 = objects.begin(); it1!= objects.end();)
    {
        if(it1->valid)
            it1 = objects.erase(it1);
        else it1++;
    }
}
vector<Candidate*> CandidateExtractor::doCouple()
{
    vector<Candidate*> candidates;
    vector<LSfeatureObject> objectsTemp= objects;

    while (objectsTemp.size()!= 0)
    {
        if (objectsTemp.size() == 1){
            vector<LSfeatureObject>::iterator it1 = objectsTemp.begin();
            candidates.insert( candidates.end() , new Candidate((*it1),(*it1),0.0) );

            objectsTemp.erase(it1);
        }
        else{
            vector<LSfeatureObject>::iterator it1 = objectsTemp.begin();
            vector<LSfeatureObject>::iterator it2 = it1;
            ++it2;

            float closest_dist;
            closest_dist=(*it1).getMiddlePoint().getDistanceFromPoint((*it2).getMiddlePoint());

            float dist;
            vector<LSfeatureObject>::iterator it3;
            it3 = it2;
            ++it2;
            for ( ; it2!= objectsTemp.end(); it2++){
                dist = (*it1).getMiddlePoint().getDistanceFromPoint((*it2).getMiddlePoint());

                if (dist <= closest_dist){
                    closest_dist = dist;
                    it3 = it2;
                }
            }
            candidates.insert(candidates.end(),new Candidate((*it1),(*it3), closest_dist));

            objectsTemp.erase(it1);
            objectsTemp.erase(it3);
        }
    }
    objects.clear();
    return candidates;
}

